<template> 
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入搜索：">
            <el-input v-model="listQuery.aname" class="input-width" placeholder="帐号/姓名" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()" style="margin-left: 20px">添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table ref="adminTable"
                :data="list"
                style="width: 100%;" border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.aid}}</template>
        </el-table-column>
        <el-table-column label="帐号" align="center">
          <template slot-scope="scope">{{scope.row.aname}}</template>
        </el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="scope">{{scope.row.grade | formatName}}</template>
        </el-table-column>
        <el-table-column label="手机号" align="center">
          <template slot-scope="scope">{{scope.row.aphone}}</template>
        </el-table-column>
        <el-table-column label="添加时间" width="160" align="center">
          <template slot-scope="scope">{{scope.row.atime | formatDateTime}}</template>
        </el-table-column>
        <!-- <el-table-column label="最后登录" width="160" align="center">
          <template slot-scope="scope">{{scope.row.loginTime | formatDateTime}}</template>
        </el-table-column> -->
        <!-- <el-table-column label="是否启用" width="140" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="handleStatusChange(scope.$index, scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status">
            </el-switch>
          </template>
        </el-table-column> -->
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <!-- <el-button size="mini"
                       type="text"
                       @click="handleSelectRole(scope.$index, scope.row)">分配角色
            </el-button> -->
            <el-button size="mini"
                       type="text"
                       @click="handleUpdate(scope.row)">
              编辑
            </el-button>
            <el-button size="mini"
                       type="text"
                       style="color:red;"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.page"
        :page-size="listQuery.pagesize"
        :page-sizes="[10,20,50]"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑密码':'添加管理员'"
      :visible.sync="dialogVisible"
      width="40%">
      <el-form :model="admin"
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item label="帐号：">
          <el-input v-model="admin.aname" :disabled="isEdit" style="max-width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="密码：">
          <el-input v-model="admin.apwd" maxlength="18" type="password" style="max-width: 300px;"></el-input>
        </el-form-item>
        <!-- <el-form-item label="姓名：">
          <el-input v-model="admin.nickName" style="max-width: 300px;"></el-input>
        </el-form-item> -->
        <el-form-item label="手机号：">
          <el-input v-model="admin.aphone" :disabled="isEdit" style="max-width: 300px;"></el-input>
        </el-form-item>
        
        <!-- <el-form-item label="备注：">
          <el-input v-model="admin.note"
                    type="textarea"
                    :rows="5"
                    style="max-width: 300px;"></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="是否启用：">
          <el-radio-group v-model="admin.status">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- <el-dialog
      title="分配角色"
      :visible.sync="allocDialogVisible"
      width="30%">
      <el-select v-model="allocRoleIds" multiple placeholder="请选择" size="small" style="width: 80%">
        <el-option
          v-for="item in allRoleList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="allocDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleAllocDialogConfirm()" size="small">确 定</el-button>
      </span>
    </el-dialog> -->
  </div>
</template>
<script>
  // import {fetchList,createAdmin,updateAdmin,updateStatus,deleteAdmin,getRoleByAdmin,allocRole} from '@/api/login';
  // import {fetchAllRoleList} from '@/api/role';
  import {fetchList,createAdmin,updateAdmin,deleteAdmin} from '@/api/login';
  import {formatDate} from '@/utils/date';

  const defaultListQuery = {
    page: 1,
    pagesize: 10,
    aname: null
  };
  const defaultAdmin = {
    // id: null,
    aname: null,
    apwd: null,
    // nickName: null,
    aphone: null,
    // note: null,
    // status: 1
  };
  export default {
    name: 'adminList',
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: null,
        total: null,
        dialogVisible: false,
        admin: Object.assign({}, defaultAdmin),
        isEdit: false,
        allocDialogVisible: false,
        allocRoleIds:[],
        allRoleList:[],
        allocAdminId:null
      }
    },
    created() {
      this.getList();
      // this.getAllRoleList();
    },
    filters: {
      formatDateTime(time) {
        if (time == null || time === '') {
          return 'N/A';
        }
        let date = new Date(time);
        return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
      },
      formatName(name) {
       return name?'超级管理员':'管理员'
      }

    },
    methods: {
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
      },
      handleSearchList() {
        this.listQuery.page = 1;
        this.getList();
      },
      handleSizeChange(val) {
        this.listQuery.page = 1;
        this.listQuery.pagesize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.page = val;
        this.getList();
      },
      handleAdd() {
        this.dialogVisible = true;
        this.isEdit = false;
        this.admin = Object.assign({},defaultAdmin);
      },
      
      // handleStatusChange(index, row) {
      //   this.$confirm('是否要修改该状态?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     updateStatus(row.id, {status: row.status}).then(response => {
      //       this.$message({
      //         type: 'success',
      //         message: '修改成功!'
      //       });
      //     });
      //   }).catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '取消修改'
      //     });
      //     this.getList();
      //   });
      // },
      // 删除
      handleDelete(row) {
        this.$confirm('是否要删除该用户?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAdmin({aid:row.aid}).then(response => {
            this.$message({
              type: 'success',
              message: response.msg
            });
            this.getList();
          });
        });
      },
      handleUpdate(row) {
        this.dialogVisible = true;
        this.isEdit = true;
        this.admin = Object.assign({},row);
      },
      // 新增or编辑
      handleDialogConfirm() {
        this.$confirm('是否要确认?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          
          if (this.isEdit) {
            if(this.admin.apwd.length<5){ 
                this.$message({
                  type: 'error',
                  message: '请输入最少5位数的密码!'
                })
                return
            }
            updateAdmin({apwd:this.admin.apwd,aid:this.admin.aid}).then(response => {
              this.$message({
                message: response.msg,
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          } else {
            createAdmin(this.admin).then(response => {
              this.$message({
                message: response.msg,
                type: 'success'
              });
              this.dialogVisible =false;
              this.getList();
            })
          }
        })
      },
      // handleAllocDialogConfirm(){
      //   this.$confirm('是否要确认?', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     let params = new URLSearchParams();
      //     params.append("adminId", this.allocAdminId);
      //     params.append("roleIds", this.allocRoleIds);
      //     allocRole(params).then(response => {
      //       this.$message({
      //         message: '分配成功！',
      //         type: 'success'
      //       });
      //       this.allocDialogVisible = false;
      //     })
      //   })
      // },
      // handleSelectRole(index,row){
      //   this.allocAdminId = row.id;
      //   this.allocDialogVisible = true;
      //   this.getRoleListByAdmin(row.id);
      // },
      getList() {
        // this.listQuery
        fetchList(this.listQuery).then(res => {
          console.log(res);
          this.list = res.obj.list;
          this.total = res.obj.count;
        });
      },
      // getAllRoleList() {
      //   fetchAllRoleList().then(response => {
      //     this.allRoleList = response.data;
      //   });
      // },
      // getRoleListByAdmin(adminId) {
      //   getRoleByAdmin(adminId).then(response => {
      //     let allocRoleList = response.data;
      //     this.allocRoleIds=[];
      //     if(allocRoleList!=null&&allocRoleList.length>0){
      //       for(let i=0;i<allocRoleList.length;i++){
      //         this.allocRoleIds.push(allocRoleList[i].id);
      //       }
      //     }
      //   });
      // }
    }
  }
</script>
<style>

</style>


